﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;
using Krystalware.SlickUpload;
using System.Net.Mail;

namespace BitPortal.Modules.Contact
{
    public partial class ContactForm : Controls.BaseUserControl
    {
        public bool EnabledCaptcha = true;
        public bool EnabledAttack = true;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            //Load Css, Js file
            (this.Page as Controls.BaseFrontendPage).IncludeCssFile("/jquery-ui.custom/css/smoothness/jquery-ui-1.8.16.custom.css");
            (this.Page as Controls.BaseFrontendPage).IncludeScriptFile("/jquery-ui.custom/js/jquery-ui-1.8.16.custom.min.js");

            base.OnInit(e);
            this.ltrTitle.Text = ReferenceModule.GetModuleConfig("Header").ToString();
            this.ltrDescription.Text = ReferenceModule.GetModuleConfig("Description").ToString();
            EnabledCaptcha = Convert.ToBoolean(ReferenceModule.GetModuleConfig("EnabledCaptcha"));
            EnabledAttack = Convert.ToBoolean(ReferenceModule.GetModuleConfig("EnabledAttack"));


        }

        protected void imgOk_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (txtName.Text.Trim().Length <= 0 || txtName.Text.Trim().Length > 100)
                {
                    throw new Exception(Resources.BitResources.Feedback_NameNotValid);
                }
                if (txtEmail.Text.Trim().Length <= 0 || txtEmail.Text.Trim().Length > 200)
                {
                    throw new Exception(Resources.BitResources.Feedback_EmailNotValid);
                }
                if (txtPhone.Text.Trim().Length > 50)
                {
                    throw new Exception(Resources.BitResources.Feedback_PhoneNotValid);
                }
                if (taAddress.Text.Trim().Length > 50)
                {
                    throw new Exception(Resources.BitResources.Feedback_AddressNotValid);
                }
                if ((Convert.ToBoolean(ReferenceModule.GetModuleConfig("EnabledCaptcha"))) && (Session["captcha"] == null || !txtcaptcha.Text.Equals(Session["captcha"].ToString(), StringComparison.OrdinalIgnoreCase)))
                {
                    throw new Exception(Resources.BitResources.Feedback_CaptchaNotValid);
                }
                Feedback feed = new Feedback()
                {
                    SenderName = txtName.Text,
                    SenderEmail = txtEmail.Text,
                    SenderPhone = txtPhone.Text,
                    SenderAddress = taAddress.Text,
                    Content = taContent.Text,
                    Subject = taSubject.Text,
                    IsReplied = false,
                    IsDelete = false,
                    DatetimeSend = DateTime.Now,
                    LanguageCode = BitContext.Current.Lang.LanguageCode
                };
                //RegisterScript("startUpload()");
                feed.AttachFile = "";
                if (UploadFile1.UploadedFiles != null)
                {
                    foreach (Subgurim.Controles.HttpPostedFileAJAX file in UploadFile1.UploadedFiles)
                    {
                        feed.AttachFile += file.FileName_Path + "-(" + file.ContentType + ")-" + "-(" + Convert.ToInt32(file.ContentLength / 1024) + " kb)<br/>";
                    }
                }
                if (feed.Insert() > 0)
                {
                    RegisterScript("ShowStatus()");
                    if (Convert.ToBoolean(ReferenceModule.GetModuleConfig("EnabledForwardToEmail")))
                    {
                        //EmailUtils.SendEmail(feed.SenderEmail, ReferenceModule.GetModuleConfig("SentToEmail").ToString(), "Phản hồi từ khách hàng: '" + feed.Subject + "'", feed.Content);
                        EmailUtils.SendEmail(from: feed.SenderEmail, to: new string[] { ReferenceModule.GetModuleConfig("SentToEmail").ToString() }, subject: "Phản hồi từ khách hàng: '" + feed.Subject + "'", body: feed.Content);
                    }
                }
                //Reset Captcha
                Session["captcha"] = null;

            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseFrontendPage).ShowArlert(ex.Message);
            }
        }

    }
}